package cn.iocoder.yudao.module.biz.controller.admin.zmdeduction;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.zmdeduction.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.zmdeduction.ZmDeductionDO;
import cn.iocoder.yudao.module.biz.service.zmdeduction.ZmDeductionService;

@Tag(name = "管理后台 - 扣款报")
@RestController
@RequestMapping("/biz/zm-deduction")
@Validated
public class ZmDeductionController {

    @Resource
    private ZmDeductionService zmDeductionService;

    @PostMapping("/create")
    @Operation(summary = "创建扣款报")
    @PreAuthorize("@ss.hasPermission('biz:zm-deduction:create')")
    public CommonResult<Long> createZmDeduction(@Valid @RequestBody ZmDeductionSaveReqVO createReqVO) {
        return success(zmDeductionService.createZmDeduction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新扣款报")
    @PreAuthorize("@ss.hasPermission('biz:zm-deduction:update')")
    public CommonResult<Boolean> updateZmDeduction(@Valid @RequestBody ZmDeductionSaveReqVO updateReqVO) {
        zmDeductionService.updateZmDeduction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除扣款报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:zm-deduction:delete')")
    public CommonResult<Boolean> deleteZmDeduction(@RequestParam("id") Long id) {
        zmDeductionService.deleteZmDeduction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得扣款报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:zm-deduction:query')")
    public CommonResult<ZmDeductionRespVO> getZmDeduction(@RequestParam("id") Long id) {
        ZmDeductionDO zmDeduction = zmDeductionService.getZmDeduction(id);
        return success(BeanUtils.toBean(zmDeduction, ZmDeductionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得扣款报分页")
    @PreAuthorize("@ss.hasPermission('biz:zm-deduction:query')")
    public CommonResult<PageResult<ZmDeductionRespVO>> getZmDeductionPage(@Valid ZmDeductionPageReqVO pageReqVO) {
        PageResult<ZmDeductionDO> pageResult = zmDeductionService.getZmDeductionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZmDeductionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出扣款报 Excel")
    @PreAuthorize("@ss.hasPermission('biz:zm-deduction:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZmDeductionExcel(@Valid ZmDeductionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZmDeductionDO> list = zmDeductionService.getZmDeductionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "扣款报.xls", "数据", ZmDeductionRespVO.class,
                        BeanUtils.toBean(list, ZmDeductionRespVO.class));
    }

}